<?php

/**
 * Gestion des utilisateurs
 *
 * @author iLyas
 */
class User_model extends CI_Model {
    
    //Login d'un user
    function validate(){    
        $this->db->where('user_pseudo', $this->input->post('username'));
        $this->db->where('user_password', sha1($this->input->post('password')));
        
        $q = $this->db->get('user');
        
        /*if ($q->num_rows == 1){
            return true;
        }
        */
         
        return (bool) $q->num_rows;
    }
    
    function create_member(){        
        if (!$this->user_exists()){
            $new_member_insert_data = array (
                'user_name'             => $this->input->post('user_name'),
                'user_pseudo'           => $this->input->post('username'),
                'user_email'            => $this->input->post('email_address'),
                'user_password'         => sha1($this->input->post('password'))
            );

            $insert = $this->db->insert('user', $new_member_insert_data);

            return $insert;
        }
   }
    
   function user_exists(){
       $this->db->where('user_email',$this->input->post('email_address'));
       $q = $this->db->get('user');
        
        return (bool) $q->num_rows;
   }

    function get_user_id($pseudo){
        $q = $this->db->query('SELECT user_id FROM user WHERE user_pseudo = "'.$pseudo.'"');
        if ($q->num_rows() > 0){
            foreach ($q->result() as $row){
                $data = $row->user_id;
            }
        }
        $q->free_result();
        return $data;
    }
    
    function get_user_lang($user_id){
        $q = $this->db->query('SELECT user_lang FROM user WHERE user_id = "'.$user_id.'"');
            $row = $q->row();
            $data = $row->user_lang;
        return $data;
    }

			
    function getUserRecords($id){
        $q = $this->db->query('SELECT * FROM user WHERE user_id = '.$id);
        
        if ($q->num_rows() > 0){
            foreach ($q->result() as $row){
                $data[] = $row;
            }
        }
        $q->free_result();
        return $data;
    }
}

?>
